Repository navigation
Fix a queued request being overwritten when the queue drains mid-conflict - #101207
Closed
adhorodyski wants to merge 21 commits into
Closed
adhorodyski wants to merge 21 commits into
adhorodyski wants to merge 21 commits into
Conversation
…ition `handleConflictActions` applied the `nextAction` replace at a queue position measured before an awaited storage commit. A drain during that commit renumbers the array, so the replace landed on an unrelated request: a queued user write was destroyed and the edited comment went out twice. `ConflictRequestReplace` now carries the `requestIndex` of the request it targets, and `PersistedRequests.update` resolves that identity to a live position at apply time. When the target is no longer queued the replace is a no-op rather than an append, which is lossless only because `resolveEditCommentWithNewAddCommentRequest` mutates the queued request in place and `processNextRequest` promotes that same object reference - the two are coupled. This is the hazard recorded as "Index fragility" in contributingGuides/SEQUENTIAL_QUEUE.md.
A nextAction replace whose target carries no requestIndex fell through to the stale positional splice, so the race stayed live on that path. ConflictRequestDelete.nextAction also still typed a follow-up delete, which applies stale indices the same way. handleConflictActions now hands a follow-up to handleFollowUpConflictAction, which applies only an identity-addressed replace and Log.alerts anything else. ConflictRequestDelete.nextAction is narrowed to ConflictRequestReplace, so a follow-up delete is a compile error even through the `as ConflictActionData` the shipped resolvers use. From the same review: PersistedRequests logs the caller's index next to the resolved one, so a caught shift is observable in production and the open frequency question becomes answerable; resolveEditCommentWithNewAddCommentRequest no longer reads persistedRequests.at(-1), which would mutate the last queued request; and SEQUENTIAL_QUEUE.md records that the in-place mutation is what makes the skip lossless, so it is not tidied away into silently losing edits.
The type narrowing is the signal a future resolver author sees, but the queue must also refuse one that arrives past the type. Against the previous commit this fixture ends with an empty queue: the stale follow-up index deletes the request the conflict was meant to keep.
…ut one npm run lint-changed reported this all along; the summary filter I was grepping through cut the line, so the reviewer's run was right and my read of my own output was wrong. The delete fixture has to violate the narrowed type to reach the branch at all, so it keeps the house one-line disable with its reason. The branch a well-typed caller can actually hit - a nextAction replace that omits requestIndex - now has its own test that needs no directive, and it is red against the previous head, where the stale index splices over the last queued request. Also restores the storage-latency note in the drain helper per the ruling, and renames two tests so the top-level case and the follow-up case read differently.
No behaviour change. handleFollowUpConflictAction took the follow-up as `conflictAction`, which read as the outer action at the call that passes `conflictAction.nextAction`. Renamed to nextAction. Its skip branch logged "no longer queued", which is wrong when a caller passes -1 and nothing was ever resolved; it now says the target is not in the queue, covering both paths. The two PersistedRequests skip cases were byte-identical apart from the update() call, so they are one it.each over the two ways a target can be missing, matching the it.each style used across tests/unit. In the queue tests the local holding the Onyx.set spy was called `commit`, which collided with the real commit promise inside the helper it wraps.
…ack-internal/Expensify-App into fm/exfy-sq-conflict-index
…the top-level replace The two refusal guards in handleFollowUpConflictAction are the same check written twice; merge them into one condition and keep nextActionType and staleIndex in the alert payload so the two refusals stay distinguishable. The top-level replace is applied synchronously, so its index is provably current: passing conflictAction.requestIndex there made update() look the target up by identity, which is the wrong answer on a cross-tab identity collision. Stop passing it and document the addressing contract on ConflictRequestReplace.requestIndex and on update().
The two refusals differ only in the fixture and the expected alert payload, so express them as two cases of one it.each and drop the now-constant alert text column. Stop stamping requestIndex on the three resolvers that address the top-level replace by index, since that path no longer reads an identity, and reword the step comments that claimed it did.
Drop the Given/When/Then step comments from the conflict tests, the doc comment on ConflictRequestReplace.requestIndex, and the one above update(). Restore the pre-existing nextAction doc comment to its original wording. Kept: the comment on the drain helper, which explains the Onyx storage-timing quirk the interception depends on, and the eslint-disable for the forced follow-up delete fixture, trimmed to the bare directive the rule needs.
This reverts commit 3ade22c.
Contributor
Author
|
@codex review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation of Change
SequentialQueue holds pending API writes and flushes them one by one, with a conflict step that rewrites the queue when a new request supersedes one already waiting in it.
The part that broke was how that step identified which queued request to replace: it used the array position, which the queue could shift while the step was waiting on a disk write, so a comment edit could overwrite a completely unrelated queued write and post itself twice. The replacement is now matched by the request's own id instead of where it sits.
Fixed Issues
$
PROPOSAL: N/A
Tests
The automated cases are the evidence here. They intercept the
ONYXKEYS.PERSISTED_REQUESTSwrite and runprocessNextRequest()inside it, so the resolver'sawaitreturns only after the queue has drained — the ordering that destroyed the victim request. A manual sequence cannot be relied on to hit a window that is milliseconds wide.npx jest tests/unit/SequentialQueueTest.tsand read theSequentialQueue - conflict replace addressingblock:should replace the request the resolver identified when the queue drained before the resolver ranshould replace the request carrying the target requestIndex after the queue renumbers under itshould keep the queued write when the resolver cannot identify the replace targetshould refuse a nextAction $name rather than apply its stale position(oneit.each, covering areplacethat omitsrequestIndexand adelete)should not replace any request once the queue has promoted the conflict target— this one also asserts the edited text on the body the queue hands tofetch, which is the last inch nobody can check by readingnpx jest tests/unit/PersistedRequests.ts tests/unit/RequestConflictUtilsTest.tsfor the addressing itself: the request carrying therequestIndexis replaced rather than the one at the caller's stale index; a target that is no longer queued, a positional-1, and an index past the end of the queue are skipped and alerted, never appended; two live requests carrying one identity are refused with an alert; and the comment conflict resolver stampsrequestIndexonto the target it returns (plus touching nothing when called with anaddCommentIndexof-1, which pins a defensive branch its only caller guards first).tests/unit/SequentialQueueTest.ts—should replace request in queue while a similar one is ongoingandshould replace request in queue while a similar one is ongoing and keep the same index, both in the plainSequentialQueueblock — characterise the unchanged positional path. They pass onmainas well, so do not count them as regression cover.src/libs/actions/PersistedRequests.ts,src/libs/actions/RequestConflictUtils.ts,src/libs/Network/SequentialQueue.tsandsrc/types/onyx/Request.tsfrommainand re-run steps 1 and 2: 13 of the 157 cases fail, includingReceived ["AddComment","AddComment"]where["AddComment","OpenReport"]is expected — the queued write destroyed and the comment going out twice.UpdateCommentfor that action, takes the unchanged top-level path, and verifies nothing here:hello, edit it tohello there, then edit that same message again tohello there now. The second edit is the one that resolves against a queuedAddCommentand a queuedUpdateComment, which is the shape that hands off thenextActionreplace.AddCommentwhen the follow-up applies.hello there nowexactly once,helloandhello therenever come back, report B's message is delivered, and the JS console has none of[SequentialQueue] Refusing a conflict follow-up that cannot be addressed by requestIndex,[PersistedRequests] Refusing to update a request whose requestIndex is carried by more than one queued request, or[PersistedRequests] Update target is in neither the queue nor the ongoing request.Offline tests
hello, edit it tohello there, edit that same message again tohello there now, then send a message in a different report B — all offline, so the queuedAddComment, the firstUpdateCommentand B's write are all in the queue when the second edit resolves against them.requestIndex, so a regression in the addressing shows up here. Nothing drains while the device is offline, so the queue cannot renumber under the resolver and the race itself cannot reproduce — step 1 is what pins that.QA Steps
Same sequence as Tests steps 5-7: edit the same message twice, with an unrelated write queued behind it. The failure window is a few milliseconds wide, so treat a manual pass as a smoke test and rely on the automated cases in the
Testssection, which force the ordering.PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectionQA Stepssection says. Ticked for the checklist, not because the box is literally true.Screenshots/Videossays the same. Ticked for the checklist, not because the box is literally true.Avatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified the component is working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
No UI change — request-queue bookkeeping only, so no screenshots or videos apply.
Android: mWeb Chrome
No UI change — request-queue bookkeeping only, so no screenshots or videos apply.
iOS: Native
No UI change — request-queue bookkeeping only, so no screenshots or videos apply.
iOS: mWeb Safari
No UI change — request-queue bookkeeping only, so no screenshots or videos apply.
MacOS: Chrome / Safari
No UI change — request-queue bookkeeping only, so no screenshots or videos apply.